﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;

namespace WindowsFormsFlower
{
    public static class RandomPoints
    {
        private static Random random = new Random();

        /// <summary>
        /// 生成随机铺满整个屏幕的坐标集合（允许重叠）
        /// </summary>
        /// <param name="count">要生成的坐标数量</param>
        /// <returns>随机分布的坐标集合</returns>
        public static List<Point> GenerateRandomPositions(int count)
        {
            Screen screen = Screen.PrimaryScreen;
            Rectangle workingArea = screen.WorkingArea;

            // 使用较小的默认窗口大小，让更多窗口能放下
            int windowWidth = workingArea.Width / 8;
            int windowHeight = workingArea.Height / 8;
            int margin = 5;

            return GenerateRandomPositions(count, windowWidth, windowHeight, margin);
        }

        /// <summary>
        /// 生成随机铺满整个屏幕的坐标集合（允许重叠）
        /// </summary>
        /// <param name="count">坐标数量</param>
        /// <param name="windowWidth">窗口宽度</param>
        /// <param name="windowHeight">窗口高度</param>
        /// <param name="margin">边距</param>
        public static List<Point> GenerateRandomPositions(int count, int windowWidth, int windowHeight, int margin = 5)
        {
            Screen screen = Screen.PrimaryScreen;
            Rectangle workingArea = screen.WorkingArea;

            List<Point> positions = new List<Point>();

            // 计算可用的最大坐标范围
            int maxX = workingArea.Width - windowWidth - margin;
            int maxY = workingArea.Height - windowHeight - margin;

            // 确保坐标范围有效
            maxX = Math.Max(margin + 1, maxX);
            maxY = Math.Max(margin + 1, maxY);

            for (int i = 0; i < count; i++)
            {
                Point newPosition = new Point(
                    random.Next(margin, maxX),
                    random.Next(margin, maxY)
                );

                positions.Add(newPosition);
            }

            return positions;
        }

        /// <summary>
        /// 生成区域化随机分布（让窗口分布在屏幕不同区域）
        /// </summary>
        public static List<Point> GenerateZonedRandomPositions(int count)
        {
            Screen screen = Screen.PrimaryScreen;
            Rectangle workingArea = screen.WorkingArea;

            int windowWidth = workingArea.Width / 8;
            int windowHeight = workingArea.Height / 8;
            int margin = 5;

            List<Point> positions = new List<Point>();

            for (int i = 0; i < count; i++)
            {
                // 随机选择屏幕区域
                var zone = random.Next(5); // 0-4: 左上、右上、左下、右下、中心
                Point position = GeneratePositionInZone(workingArea, windowWidth, windowHeight, margin, zone);
                positions.Add(position);
            }

            return positions;
        }

        /// <summary>
        /// 生成带权重随机分布（某些区域窗口更密集）
        /// </summary>
        public static List<Point> GenerateWeightedRandomPositions(int count)
        {
            Screen screen = Screen.PrimaryScreen;
            Rectangle workingArea = screen.WorkingArea;

            int windowWidth = workingArea.Width / 8;
            int windowHeight = workingArea.Height / 8;
            int margin = 5;

            List<Point> positions = new List<Point>();

            for (int i = 0; i < count; i++)
            {
                // 中心区域权重更高（60%概率在中心区域）
                int zone = random.Next(100) < 60 ? 4 : random.Next(4);
                Point position = GeneratePositionInZone(workingArea, windowWidth, windowHeight, margin, zone);
                positions.Add(position);
            }

            return positions;
        }

        /// <summary>
        /// 生成渐进式随机分布（从中心向外扩散）
        /// </summary>
        public static List<Point> GenerateProgressiveRandomPositions(int count)
        {
            Screen screen = Screen.PrimaryScreen;
            Rectangle workingArea = screen.WorkingArea;

            int windowWidth = workingArea.Width / 8;
            int windowHeight = workingArea.Height / 8;
            int margin = 5;

            List<Point> positions = new List<Point>();
            Point center = new Point(workingArea.Width / 2, workingArea.Height / 2);

            for (int i = 0; i < count; i++)
            {
                // 随着i增加，逐渐扩大随机范围
                int maxDistance = Math.Min(workingArea.Width, workingArea.Height) * (i + 1) / (count * 2);

                Point position = GeneratePositionAroundPoint(workingArea, center, maxDistance, windowWidth, windowHeight, margin);
                positions.Add(position);
            }

            return positions;
        }

        /// <summary>
        /// 在指定区域生成随机位置
        /// </summary>
        private static Point GeneratePositionInZone(Rectangle workingArea, int width, int height, int margin, int zone)
        {
            int zoneWidth = workingArea.Width / 3;
            int zoneHeight = workingArea.Height / 3;

            int minX, maxX, minY, maxY;

            switch (zone)
            {
                case 0: // 左上
                    minX = margin;
                    maxX = zoneWidth - width - margin;
                    minY = margin;
                    maxY = zoneHeight - height - margin;
                    break;
                case 1: // 右上
                    minX = workingArea.Width - zoneWidth + margin;
                    maxX = workingArea.Width - width - margin;
                    minY = margin;
                    maxY = zoneHeight - height - margin;
                    break;
                case 2: // 左下
                    minX = margin;
                    maxX = zoneWidth - width - margin;
                    minY = workingArea.Height - zoneHeight + margin;
                    maxY = workingArea.Height - height - margin;
                    break;
                case 3: // 右下
                    minX = workingArea.Width - zoneWidth + margin;
                    maxX = workingArea.Width - width - margin;
                    minY = workingArea.Height - zoneHeight + margin;
                    maxY = workingArea.Height - height - margin;
                    break;
                default: // 中心
                    minX = zoneWidth + margin;
                    maxX = workingArea.Width - zoneWidth - width - margin;
                    minY = zoneHeight + margin;
                    maxY = workingArea.Height - zoneHeight - height - margin;
                    break;
            }

            // 确保范围有效
            maxX = Math.Max(minX + 1, maxX);
            maxY = Math.Max(minY + 1, maxY);

            return new Point(
                random.Next(minX, maxX),
                random.Next(minY, maxY)
            );
        }

        /// <summary>
        /// 围绕指定点生成随机位置
        /// </summary>
        private static Point GeneratePositionAroundPoint(Rectangle workingArea, Point center, int maxDistance, int width, int height, int margin)
        {
            // 随机角度和距离
            double angle = random.NextDouble() * 2 * Math.PI;
            double distance = random.NextDouble() * maxDistance;

            int x = center.X + (int)(Math.Cos(angle) * distance);
            int y = center.Y + (int)(Math.Sin(angle) * distance);

            // 确保在屏幕范围内
            x = Math.Max(margin, Math.Min(x, workingArea.Width - width - margin));
            y = Math.Max(margin, Math.Min(y, workingArea.Height - height - margin));

            return new Point(x, y);
        }

        /// <summary>
        /// 生成集群式随机分布（形成几个集群）
        /// </summary>
        public static List<Point> GenerateClusteredRandomPositions(int count, int clusterCount = 3)
        {
            Screen screen = Screen.PrimaryScreen;
            Rectangle workingArea = screen.WorkingArea;

            int windowWidth = workingArea.Width / 8;
            int windowHeight = workingArea.Height / 8;
            int margin = 5;

            List<Point> positions = new List<Point>();
            List<Point> clusterCenters = new List<Point>();

            // 生成集群中心点
            for (int i = 0; i < clusterCount; i++)
            {
                clusterCenters.Add(new Point(
                    random.Next(margin, workingArea.Width - windowWidth - margin),
                    random.Next(margin, workingArea.Height - windowHeight - margin)
                ));
            }

            // 为每个点分配集群
            for (int i = 0; i < count; i++)
            {
                Point clusterCenter = clusterCenters[random.Next(clusterCount)];
                int clusterRadius = Math.Min(workingArea.Width, workingArea.Height) / 6;

                Point position = GeneratePositionAroundPoint(workingArea, clusterCenter, clusterRadius, windowWidth, windowHeight, margin);
                positions.Add(position);
            }

            return positions;
        }
    }
}